﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using G.WeiBo.WebAPI;
using G.WeiBo.WebAPI.Response;
using G.WeiBo.WebAPI.Request;
using G.WeiBo.WebAPI.Domain;
using System.Collections.ObjectModel;
using System.IO;

namespace G.WeiBo.View
{
    public partial class Index : GBasePage
    {
        //private static bool IsRegistedNavigating;
        DefaultClient client = GlobalStaticData.Client;
        public Index()
        {
            InitializeComponent();
            //if (!IsRegistedNavigating)
            //{
            //    (Application.Current as App).RootFrame.Navigating += new System.Windows.Navigation.NavigatingCancelEventHandler(RootFrame_Navigating);
            //    IsRegistedNavigating = true;
            //}
        }


        //void RootFrame_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        //{
        //    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back && e.Uri.OriginalString == "/MainPage.xaml")
        //    {
        //        if ((Application.Current as App).RootFrame.Content == this)
        //        {
        //            if (MessageBox.Show("是否要退出程序？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        //            {
        //                Application.Current.ExitApplication();
        //            }
        //            else
        //            {
        //                e.Cancel = true;
        //            }
        //        }
        //    }
        //}


        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
            {
                var list = this.NavigationService.BackStack.ToArray();
                foreach (var item in list)
                {
                    this.NavigationService.RemoveBackEntry();
                }
            }
            //this.NavigationService.RemoveBackEntry();
        }



        private void listbox_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            ObservableCollection<Message> itemsSource = this.listbox.ItemsSource as ObservableCollection<Message>;
            if (itemsSource != null && itemsSource.Count > 0)
            {
                client.Execute(new GetFriendsTimelineRequest { Count = e.PageSize, Page = e.PageNum, Max_id = itemsSource.Last().Id }, (c) =>
                {
                    this.listbox.LoadData<Message>(c.Messages);
                });
            }
            else
            {
                client.Execute(new GetFriendsTimelineRequest { Count = e.PageSize, Page = e.PageNum }, (c) =>
                {
                    this.listbox.LoadData<Message>(c.Messages);
                });
            }
        }

        private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetData();
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listbox.SelectedItem != null)
            {
                this.NavigationService.Navigate("/View/CommentsList.xaml", listbox.SelectedItem);
                listbox.SelectedItem = null;
            }
        }

        private void GetData()
        {
            if (pivot.SelectedIndex == 0 && this.listbox.ItemsSource == null)
            {
                this.listbox.EnableTombstoneListBox<Message>(this);
            }
            //else if (pivot.SelectedIndex == 1 && this.listboxPublic.ItemsSource == null)
            //{
            //    this.listboxPublic.EnableTombstoneListBox<Message>(this);
            //}

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (pivot.SelectedIndex == 0)
            {
                listbox.ClearItems();
            }
            //else if (pivot.SelectedIndex == 1)
            //{
            //    listboxPublic.ClearItems();
            //}
            GetData();

        }

        private void btnWriteWeiBo_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate("/View/WriteWeiBoPage.xaml", null);
        }

        private void btnMyPage_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate("/View/MyPage.xaml", null);
        }

        private void menuTopic_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/View/TopicPage.xaml", UriKind.Relative));
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/View/TopicBrowserPage.xaml", UriKind.Relative));
        }

        private void menuSetting_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/View/SettingPage.xaml", UriKind.Relative));
        }

        private void menuDownloading_Click(object sender, EventArgs e)
        {
            this.listbox.ClearItems();
            this.listbox.CancelPageListBox();
            this.listbox.PageSize = 200;
            this.listbox.EnableTombstoneListBox<Message>(this);
            this.listbox.PageSize = 20;
            //client.Execute<GetFriendsTimelineResponse>(new GetFriendsTimelineRequest { Count = 200, Page = 0 }, (c) =>
            //{
            //    this.Dispatcher.BeginInvoke(() =>
            //    {
            //        this.listbox.LoadData<Message>(c.Messages);
            //    });
            //});
        }

        private void menuScrollToFirst_Click(object sender, EventArgs e)
        {
            this.listbox.ScrollIntoView(this.listbox.GetItemsSource<Message>().FirstOrDefault());
        }

        private void listboxPublic_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            //ObservableCollection<Message> itemsSource = this.listbox.ItemsSource as ObservableCollection<Message>;
            //if (itemsSource != null && itemsSource.Count > 0)
            //{
            //    client.Execute<GetFriendsTimelineResponse>(new GetFriendsTimelineRequest { Count = e.PageSize, Page = e.PageNum, Max_id = itemsSource.Last().Id }, (c) =>
            //    {
            //        this.Dispatcher.BeginInvoke(() =>
            //        {
            //            this.listbox.LoadData<Message>(c.Messages);
            //        });
            //    });
            //}
            //else
            //{
            //client.Execute<GetPublicTimelineResponse>(new GetPublicTimelineRequest { Count = e.PageSize, Page = e.PageNum }, (c) =>
            //{
            //    this.Dispatcher.BeginInvoke(() =>
            //    {
            //        this.listboxPublic.LoadData<Message>(c.Messages);
            //    });
            //});
            //}
        }

        private void menuReset_Click(object sender, EventArgs e)
        {
            this.listbox.UpdateLayout();
            this.listbox.InvalidateArrange();
        }

        private void menuLogout_Click(object sender, EventArgs e)
        {

            //var stream = App.GetResourceStream(new Uri("/G.WeiBo;component/1永恒之井.txt", UriKind.Relative)).Stream;
            //byte[] buffer = stream.GetBytes();
            //G.Net.HttpPost post = new Net.HttpPost("http://dagehaoshuang.31.cvod.net/EncodingConvert.ashx");
            //post.PostData = buffer;
            //var time1 = DateTime.Now;
            //post.Request((s, msg) =>
            //{
            //    var time2 = DateTime.Now;
            //    var sp = time2 - time1;
            //    var saaaa = msg;
              
            //});
            //return;
            G.Net.Oauth.Oauth2Valid.Logout(APIConfig.AppName);
            //NavigatingCancelHandler eh = null;
            //eh = (a, b) =>
            //{
            //    var list = this.NavigationService.BackStack.ToArray();
            //    foreach (var item in list)
            //    {
            //        this.NavigationService.RemoveBackEntry();
            //    }
            //    this.NavigatingFrom -= eh;
            //};
            //this.NavigatingFrom += eh;
            this.NavigationService.Navigate("/MainPage.xaml");
        }
    }
}